package com.hanwei.uap.acceptors.sstn.coder;

import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

public class MessageDecoder extends ByteToMessageDecoder {
	@Override
	protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
		int start = 0;
		in.markReaderIndex();
		while (in.readableBytes() > 0) {
			if (in.readByte() == (byte) 0x0D) {
				int length = in.readerIndex()-start;
				byte[] msg = new byte[length];
				in.resetReaderIndex();
				in.readBytes(msg);
				out.add(msg);
				start = in.readerIndex();
				in.markReaderIndex();
			}
		}
		in.resetReaderIndex();
	}
}
